//
//  UIColor.swift
//  readtool
//
//  Created by WanJiading on 16/6/14.
//  Copyright © 2016年 DAD. All rights reserved.
//

import UIKit

extension UIColor {

    /**
     根据rgb返回 UIColor
	
	 - parameter hexString: "84A740", 不要传0x84A740 | #84A740
     - parameter alphe:     透明度
     */
    class func rgb(_ rgbString: String, alpha: CGFloat) -> UIColor {
		
		let cString: String = rgbString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()

        let nsRGBStr = cString as NSString
        var range: NSRange = NSMakeRange(0, 2)
        let rString = nsRGBStr.substring(with: range)
            range.location = 2
        let gString = nsRGBStr.substring(with: range)
            range.location = 4
        let bString = nsRGBStr.substring(with: range)

        var r: UInt32 = 0x0
        var g: UInt32 = 0x0
        var b: UInt32 = 0x0
        Scanner.init(string: rString).scanHexInt32(&r)
        Scanner.init(string: gString).scanHexInt32(&g)
        Scanner.init(string: bString).scanHexInt32(&b)
        
        return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: CGFloat(alpha))
    }
	
	/**
	快速根据rgb返回 UIColor
	
	- parameter hexString: "84A740", 不要传0x84A740 | #84A740
	*/
	class func rgb(_ rgbString: String) -> UIColor {
		return self.rgb(rgbString, alpha: 1.0)
	}
	
	class var random: UIColor {
		let red = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
		let green = CGFloat( arc4random_uniform(255))/CGFloat(255.0)
		let blue = CGFloat(arc4random_uniform(255))/CGFloat(255.0)
		return UIColor.init(red:red, green:green, blue:blue , alpha: 1)
	}
    
    
    /// 生成渐变色
    class func gradient(_ colors: [UIColor], _ type: GuadientType, _ size: CGSize) -> UIColor {
        return UIColor(patternImage: UIImage.gradientColorImage(colors, type, size))
    }
}
